<?php namespace App\Core\Repository\Criterias;

use App\Core\Repository\Criteria;
use App\Core\Repository\Repository;

class BetweenCriteria extends Criteria {
    
    protected $field;
    protected $min;
    protected $max;
    protected $includeMin;
    protected $includeMax;

    public function __construct($field, $min, $max, $includeMin = true, $includeMax = false) {
        $this->field = $field;
        $this->min = $min;
        $this->max = $max;
        $this->includeMin = $includeMin;
        $this->includeMax = $includeMax;
        $this->setName('between_'.$this->field);
    }

    public function apply(Repository $repository){
        parent::apply($repository);
        $field = $this->formatField($this->model, $this->field);
        $this->query->where($field, ($this->includeMin ? '>=' : '>'), $this->min);
        $this->query->where($field, ($this->includeMax ? '<=' : '<'), $this->max);
    }
}